﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OperationLibrary;

namespace 计算器窗体
{
    /// <summary>
    ///  面向对象编程
    /// 1、加运算类
    /// 2、加运算子类
    /// 3、加运算类工厂
    /// 4、更改Form1
    /// </summary>
    public partial class Form1 : Form
    {
        Operation oper;
        bool bOpertion = false; //指示是否按了运算符
        public Form1()
        {
            InitializeComponent();
        }
        //数字键和.
        private void button0_Click(object sender, EventArgs e)
        {
            if(bOpertion )  //如果已输入了运算符。则文本框清空。
            {
                txtShow.Text = "";
                bOpertion = false;
            }
            string number = ((Button)sender).Text;
            txtShow.Text = Operation.checkNumberInput(txtShow.Text, number);
        }
        //加减乘除
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
               oper =OperationFactory.createOperation(((Button)sender).Text);
               oper.NumberA = Convert.ToDouble(txtShow.Text);
               bOpertion = true;
            }

        }
        //开方 平方 取反 等于
        private void buttonEqual_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                if (((Button)sender).Text != "=")
                {
                    oper = OperationFactory.createOperation(((Button)sender).Text);
                }
                oper.NumberB = Convert.ToDouble(txtShow.Text);//单目运算，只考虑NumberB

                txtShow.Text = oper.GetResult().ToString();

                bOpertion = true;
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            txtShow.Text = "";
        }
    }
}
